package cn.itcast.web.controller.cargo;

import cn.itcast.dao.cargo.InvoiceDao;
import cn.itcast.domain.cargo.Finance;
import cn.itcast.domain.cargo.FinanceExample;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.InvoiceExample;
import cn.itcast.service.cargo.FinanceService;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.vo.ContractProductVo;
import cn.itcast.vo.FinanceProductVo;
import cn.itcast.vo.FinanceVo;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/cargo/finance")
public class FinanceController extends BaseController {

   @Reference
    private FinanceService financeService;

   @Reference
   private InvoiceService invoiceService;

   /*
        查询财务报运单
    */
    @RequestMapping("/list")
    public String findAll(@RequestParam(defaultValue = "1")int page,
                          @RequestParam(defaultValue = "5")int size){
        //1、根据当前企业查询所有报运但数据
        FinanceExample example = new FinanceExample();
        FinanceExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        //调用financeService层查询
        PageInfo info = financeService.findAll(page, size, example);
        //将数据保存到request域中
        request.setAttribute("page",info);
        return "cargo/finance/finance-list";
    }

    /*
        新增财务报运单
        地址：/cargo/finance/toAdd.do
     */
    @RequestMapping("/toAdd")
    public String toAdd(@RequestParam(defaultValue = "1")int page,
                        @RequestParam(defaultValue = "5")int size){
        //设置查询财务发票状态
        InvoiceExample example = new InvoiceExample();
        InvoiceExample.Criteria criteria = example.createCriteria();
        criteria.andStateEqualTo(1);
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        //查询所有状态为1的财务发票
        List<Invoice> list = invoiceService.findAll(example);
        //保存到request域中
        request.setAttribute("list",list);
        return "cargo/finance/finance-add";
    }

    /*
        保存财务报运单
        地址：/cargo/finance/edit.do
     */
    @RequestMapping("/edit")
    public String edit(Finance finance){
       //封装当前用户企业信息
        finance.setCompanyId(getLoginCompanyId());
        finance.setCompanyName(getLoginCompanyName());
        //调用financeService保存财务报运单数据
        financeService.save(finance);
        return "redirect:/cargo/finance/list.do";
    }

    /*
        使用Excel下载财务报运单数据
        地址：/cargo/finance/printTemplate.do
        参数：财务报运单Id
     */
    @RequestMapping("/printTemplate")
    public void printTemplate(String id) throws IOException {
        //1、构造数据
            //根据报运单ID获取报运单数据（报运单表头数据）
       Map<String, String> map = financeService.findFinance(id);
        //查询报运合同下的所有货物商品
        List<FinanceProductVo> list= financeService.findFinancePrdout(id);
        //加载模板信息
        String path = session.getServletContext().getRealPath("/")+"/8-财务12JX295.xlsx";
        //3、设置EasyExcel的文件下载头信息
        response.setContentType("application/vnd.ms-excel"); //下载excel
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("财务报运单", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //4、调用工具类创建Excel对象
        ExcelWriter build = EasyExcel.write(response.getOutputStream()) //response的输出输出流
                .head(FinanceProductVo.class) //设置表头的对象字节码
                .withTemplate(path)
                .build();
        //5、获取页对象
        WriteSheet sheet = EasyExcel.writerSheet().build();
        //6、填充map数据
        build.fill(map, sheet);
        //7、填充list数据
        build.fill(list, sheet);
        //8.属性资源，完成下载
        build.finish(); //下载excel文件，释放内存资源
    }
}
